#!/bin/bash

nginx=/opt/module/nginx/sbin/nginx
log_home=/opt/gmall211025
jar=gmall-logger-0.0.1-SNAPSHOT.jar

# 在hadoop162上启动nginx

# 在 hadoop162-164上启动日志服务器

case $1 in
"start")
    if [[ -z "`pgrep nginx`" ]]; then
        echo "在hadoop162上启动nginx"
        $nginx
    else
        echo "在hadoop162上已经启动nginx, 无序重复启动..."
    fi

    for host in hadoop162 hadoop163 hadoop164 ; do

        a=`ssh $host "jps | awk '/$jar/{print \$N}'"`
        if [[ -z "$a" ]]; then
            echo "在 $host 上启动日志服务器"
            ssh $host "nohup java -jar $log_home/$jar >/dev/null 2>&1  &"
        else

            echo " $host 上的日志服务器已经启动...."
        fi

    done

       ;;
"stop")


    if [[ -z "`pgrep nginx`" ]]; then
        echo "nginx 已经停止"
    else
        echo "在hadoop162上停止nginx"
        $nginx -s stop
    fi

    for host in hadoop162 hadoop163 hadoop164 ; do
        echo "在 $host 上停止日志服务器"
        ssh $host "jps | awk '/$jar/{print \$1}' | xargs kill -9"
    done

    ;;

*)
    echo "你使用的姿势不对:  "
    echo "log.sh start 启动日志采集  "
    echo "log.sh stop  停止日志采集  "
;;
esac